#include <iostream>
#include <queue>
using namespace std;

class Point{
public:
    pair<int, int> p;
    int path;
    Point(){}
    Point(int x, int y, int path){this->p = pair<int, int>(x, y); this->path = path;}
};
int dir[4][2] = {0,1, 0,-1, 1,0, -1,0};
int bfs(vector<string>mp, int xs, int ys, int xt, int yt, int n, int m){
    queue<Point> q;
    q.emplace(Point(xs, ys, 0));
    while(!q.empty()){
        auto tmp = q.front();
        q.pop();
        for(int i =0; i < 4; i++){
            int dx = tmp.p.first + dir[i][0];
            int dy = tmp.p.second + dir[i][1];
            int path = tmp.path;
            if (dx < 0 || dx >= n || dy < 0 || dy >= m) continue;
            else if (dx == xt && dy == yt && mp[dx][dy] == '.'){
                return path + 1;
            } else if(mp[dx][dy] == '.'){
                q.emplace(Point(dx, dy, path + 1));
                mp[dx][dy] = '*';
            } else if (mp[dx][dy] == '*'){
                continue;
            }
        }
    }
    return -1;
}
int main() {
    int n,m;
    int xs,ys,xt,yt;
    while (cin>>n>>m) {
        cin>>xs>>ys>>xt>>yt;
        string s;
        vector<string>mp;
        for(int i = 0; i < n; i++){
            cin>>s;
            mp.emplace_back(s);
        }
        int path = bfs(mp, xs-1, ys-1, xt-1, yt-1, n, m);
        cout<<path<<endl;
    }
}
// 64 位输出请用 printf("%lld")